package com.user.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import com.user.entity.Department;

public class DepartmentDTO {
	public static Specification<Department> getSpecification(String str) {
		Specification<Department> spec = new Specification<Department>() {
			public Predicate toPredicate(Root<Department> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				/*List<Predicate> list = new ArrayList<Predicate>();
				Predicate p1 = cb.isNotNull(root.get("releaseDate"));
				list.add(p1);
				Predicate[] p = new Predicate[list.size()];*/
				List<Predicate> list = new ArrayList<Predicate>();
				Predicate[] p = new Predicate[list.size()];
				if(null!=str&&!"".equals(str)) {
					Predicate  predicate1 =  cb.like(root.get("name").as(String.class),"%"+str+"%");
					list.add(predicate1);
					Predicate  predicate2 =  cb.like(root.get("num").as(String.class),"%"+str+"%");
					list.add(predicate2);
					
					return cb.or(list.toArray(p));
				}else {
					return cb.and(list.toArray(p));
				}
				
				
			}
		};
		return spec;
	}
}
